<template>
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="true" :title="title" :type="type" v-model="isVisible" :width="width" top="50px" append-to-body>
        <el-form ref="dictRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="字典名称" prop="dictName">
                <el-input v-model="form.dictName" placeholder="请输入字典名称" />
            </el-form-item>
            <el-form-item label="字典类型" prop="dictType">
                <el-input v-model="form.dictType" placeholder="请输入字典类型" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                    <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup lang="ts" name="DictionaryEdit">
    import { addType, updateType } from "@/api/system/dict/type";
    import { ref, computed, getCurrentInstance, watch } from "vue";
    const { proxy } = getCurrentInstance() as any;
    const props = defineProps({
        dialogVisible: {
            type: Boolean,
            default: false,
        },
        type: {
            type: String,
            default: "add",
        },
        dictType: {
            type: Object,
            default: {},
        },
    });
    const emit = defineEmits(["success"]);
    const screenWidth = ref(0);
    const width = ref(initWidth());
    const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
    const rules = ref({
        dictName: [{ required: true, message: "字典名称不能为空", trigger: "blur" }],
        dictType: [{ required: true, message: "字典类型不能为空", trigger: "blur" }],
    });

    const form = ref(props.dictType);
    watch(
        () => props.dictType,
        () => {
            form.value = props.dictType;
        },
        {
            deep: true,
        }
    );
    const isVisible = computed({
        get: () => props.dialogVisible,
        set: (val) => {
            close();
            reset();
        },
    });
    const title = computed(() => (props.type === "add" ? "新增" : "修改"));

    window.onresize = () => {
        return (() => {
            width.value = initWidth();
        })();
    };

    function initWidth() {
        screenWidth.value = document.body.clientWidth;
        if (screenWidth.value < 991) {
            return "90%";
        } else if (screenWidth.value < 1400) {
            return "45%";
        } else {
            return "800px";
        }
    }

    function close() {
        emit("close");
        reset();
    }
    /** 表单重置 */
    function reset() {
        form.value = {
            dictId: undefined,
            dictName: undefined,
            dictType: undefined,
            status: "0",
            remark: undefined,
        };
        proxy.resetForm("dictRef");
    }
    /** 提交按钮 */
    function submitForm() {
        proxy.$refs["dictRef"].validate((valid: any) => {
            if (valid) {
                if (form.value.dictId != undefined) {
                    updateType(form.value).then((response) => {
                        proxy.$modal.msgSuccess("修改成功");
                        emit("success");
                        close();
                    });
                } else {
                    addType(form.value).then((response) => {
                        proxy.$modal.msgSuccess("新增成功");
                        emit("success");
                        close();
                    });
                }
            }
        });
    }
</script>
<style lang="scss" scoped></style>
